package com.hc.controller;

import com.hc.bean.User;
import com.hc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("用户管理api")
@RestController
@RequestMapping("/user2")
public class UserController2 {

    /*
    * The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)
	- @org.springframework.beans.factory.annotation.Qualifier(value=userServiceImpl2)
    * */
    @Autowired
    @Qualifier("userServiceImpl2")
//   @Resource(name = "userServiceImpl2")
    private UserService userService;

    @ApiOperation("查询所有用户信息")
    @RequestMapping("/getUser")
    public List<User> getUser(){
        return userService.findAll();
    }

    @ApiOperation("根据name查询所有用户信息")
    @RequestMapping("/getUserByName")
    public Map getUserByName(String name){
        Map resultMap=new HashMap();
        List<User> userList=userService.findUserByName(name);
        if(userList.size()>0){
            resultMap.put("success",true);
            resultMap.put("message","查询成功");
            resultMap.put("userList",userList);
        }else {
            resultMap.put("success",false);
            resultMap.put("message","查询失败");
        }
        return resultMap;
    }

    @RequestMapping("/addUser")
    public Map addUser(User user){
        Map resultMap=new HashMap();
        if(userService.addUser(user)>0){
            resultMap.put("success",true);
            resultMap.put("message","添加成功");
        }else {
            resultMap.put("success",false);
            resultMap.put("message","添加失败");
        }
        return resultMap;
    }

    @RequestMapping("/updateUser")
    public Map updateUser(User user){
        Map resultMap=new HashMap();
        if(userService.updateUser(user)>0){
            resultMap.put("success",true);
            resultMap.put("message","更新成功");
        }else {
            resultMap.put("success",false);
            resultMap.put("message","更新失败");
        }
        return resultMap;
    }

    @RequestMapping("/deleteUser")
    public Map deleteUser(Integer id){
        Map resultMap=new HashMap();
        if(userService.deleteUser(id)>0){
            resultMap.put("success",true);
            resultMap.put("message","删除成功");
        }else {
            resultMap.put("success",false);
            resultMap.put("message","删除失败");
        }
        return resultMap;
    }

}
